package com.ruoyi.wxApp.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wxApp.domain.DisWxuser;
import com.ruoyi.wxApp.service.WxLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.net.UnknownHostException;

@Anonymous
@RestController
@RequestMapping("/wx")
public class WxLoginController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private WxLoginService disWxuserService;


    @PostMapping("/wxLogin")
    public AjaxResult wxLogin(@RequestBody String codeJson) throws UnknownHostException {
        AjaxResult ajax = AjaxResult.success();
        JSONObject json = JSONObject.parseObject(codeJson);
        String code = json.getString("code");
        //用自己的小程序信息
        System.out.println(code);
        String openid = disWxuserService.getWxOpenid(code);
        if (openid == null) {
            return AjaxResult.success("获取微信信息失败,请重新登录").put("state","-1");
        }
        SysUser userPast = userService.selectUserByWxOpenid(openid);
//        添加user
        if (userPast == null) {
            int wxId = disWxuserService.setDefaultWxuser(openid);
            SysUser user = new SysUser();
            // 生成随机字符串作为用户名的一部分
            String randomStr = UUID.randomUUID().toString().substring(0, 8); // 截取UUID的前8位作为随机字符串
            user.setUserName("WxUser" + randomStr);
            user.setPassword(SecurityUtils.encryptPassword("111111"));
            user.setNickName("WxUser_"+ randomStr);
            user.setAvatar("/profile/avatar/2024/08/15/ruoyi_20240815180206A003.jpg");
            user.setWxOpenid(openid);
            userService.insertUser(user);

            SysUser sysUser = userService.selectUserByWxOpenid(openid);
            /*wxuser = disWxuserService.selectDisWxuserByWxOpenid(openid);
            ajax.put("wechatInfo", wxuser);*/
            ajax.put("user", sysUser);
            return ajax;
        }
        //正常登录逻辑
        SysUser wxUser =userService.selectUserByWxOpenid(openid);
        if (wxUser == null) {
            return AjaxResult.success("获取微信信息失败,请重新登录");
        }
        else {
            /*ajax.put("wechatInfo", wxuser);*/
            ajax.put("userName", wxUser.getUserName());
            ajax.put("wxOpenid", wxUser.getWxOpenid());
            ajax.put("userNickName", wxUser.getNickName());
            ajax.put("userId", wxUser.getUserId());
            ajax.put("userAvatar", wxUser.getAvatar());
            ajax.put("userCreateTime", wxUser.getCreateTime());
            return ajax;
        }
    }

    /**
     * 修改头像
     */
    @PostMapping("/update")
    public AjaxResult update(DisWxuser disWxuser) throws UnknownHostException {
        disWxuserService.updateDisWxuser(disWxuser);
        return AjaxResult.success();
    }

}
